<template>
  <div>
    <el-input v-model="message"></el-input>
    <el-button @click="send">发送</el-button>
  </div>
</template>

<script>
export default {
  name: 'WebSocketTest',
  data () {
    return {
      webSocket: null,
      message: ''
    }
  },
  mounted () {
    window.axios = this.$http
    this.init()
  },
  methods: {
    init () {
      const url = `ws://${location.host}${process.env.VUE_APP_SERVER_URL}/test/websocket`
      this.webSocket = new WebSocket(url)
      this.webSocket.onopen = this.onOpen
    },
    onOpen () {
      console.log('WebSocket 连接成功')
    },
    onError () {
    },
    send () {
      this.webSocket.send(this.message)
    },
    onMessage (e) {
      console.log('WebSocket 接收到一条消息：' + e.data)
    },
    close () {
      console.log('WebSocket close')
    }
  }
}
</script>

<style scoped>

</style>
